﻿using System.Text;
using System.Web.UI;

namespace Hematology.Tools
{
	public class ValidationHelper
	{
		public static void RegisterServerValidationMessageScript(TemplateControl ctrl, string validationGroup)
		{
			if (ctrl != null && ctrl.Page.IsPostBack)
			{
				ctrl.Page.Validate(validationGroup);
				if (!ctrl.Page.IsValid)
				{
					StringBuilder errorMessage = new StringBuilder("<script language='javascript'>alert('");
					for (int i = 0; i < ctrl.Page.Validators.Count; i++)
					{
						IValidator validator = ctrl.Page.Validators[i];
						if (!validator.IsValid)
						{
							errorMessage.AppendFormat("- {0}", validator.ErrorMessage);
							if (i < ctrl.Page.Validators.Count - 1)
							{
								errorMessage.Append(@"\r\n");
							}
						}
					}

					errorMessage.Append("');</script>");
					ctrl.Page.ClientScript.RegisterStartupScript(typeof(IValidator), "validationAlert", errorMessage.ToString(), false);
				}
			}
		}
	}
}
